package com.yu.flow.test;

import com.yu.flow.demo.*;

import java.util.Collections;
import java.util.Map;

public class BusinessProcessOrchestrationFramework {


    public static void main(String[] args) {
        // 创建工作流引擎
        WorkflowEngine engine = new WorkflowEngine(new SyncTaskExecutor());

        // 定义工作流
        WorkflowDefinition workflow = new WorkflowDefinition("OrderProcessingWorkflow");

        // 定义任务
        TaskConfig validateOrder = TaskConfig.builder("validateOrder", context -> {
                    System.out.println("Validating order...");
                    // 模拟订单验证
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    return true;
                })
                .outputKey("isValid")
                .build();

        TaskConfig processPayment = TaskConfig.builder("processPayment", context -> {
                    System.out.println("Processing payment...");
                    boolean isValid = (Boolean) context.getData("isValid");
                    if (!isValid) {
                        throw new RuntimeException("Invalid order");
                    }
                    // 模拟支付处理
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    return "PAYMENT_SUCCESS";
                })
                .dependencies(Collections.singletonList("validateOrder"))
                .inputMapping(Collections.singletonMap("isValid", "isValid"))
                .outputKey("paymentStatus")
                .retries(3)
                .build();

        TaskConfig prepareOrder = TaskConfig.builder("prepareOrder", context -> {
                    System.out.println("Preparing order...");
                    String paymentStatus = (String) context.getData("paymentStatus");
                    if (!"PAYMENT_SUCCESS".equals(paymentStatus)) {
                        throw new RuntimeException("Payment failed");
                    }
                    // 模拟订单准备
                    try {
                        Thread.sleep(1500);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    return "ORDER_READY";
                })
                .dependencies(Collections.singletonList("processPayment"))
                .inputMapping(Collections.singletonMap("paymentStatus", "paymentStatus"))
                .build();

        TaskConfig shipOrder = TaskConfig.builder("shipOrder", context -> {
                    System.out.println("Shipping order...");
                    // 模拟订单发货
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    return "ORDER_SHIPPED";
                })
                .dependencies(Collections.singletonList("prepareOrder"))
                .build();

        // 添加任务到工作流
        workflow.addTask(validateOrder);
        workflow.addTask(processPayment);
        workflow.addTask(prepareOrder);
        workflow.addTask(shipOrder);

        // 执行工作流
        WorkflowContext context = engine.executeWorkflow(workflow, Collections.singletonMap("orderId", "ORD12345"));

        // 输出结果
        System.out.println("Workflow execution completed with ID: " + context.getExecutionId());
        System.out.println("Final results:");
//        for (Map.Entry<String, Object> entry : context.getData().entrySet()) {
//            System.out.println(entry.getKey() + ": " + entry.getValue());
//        }
    }
}
